package cn.zl.ZLM.event.callback.impl;

import cn.zl.ZLM.core.ZLMApi;
import cn.zl.ZLM.event.callback.inter.IMKStreamChangeCallBack;
import cn.zl.ZLM.helper.ZLMApiHelper;
import cn.zl.ZLM.structure.MK_MEDIA_SOURCE;
import cn.yunbamboos.Const;
import cn.zl.common.ZLConst;
import cn.zl.common.manage.StreamManage;
import cn.yunbamboos.mybatis.conditions.Conditions;
import cn.zl.mapper.StreamProxyMapper;
import cn.zl.mapper.StreamPushMapper;
import cn.zl.mapper.VirtualVideoStreamMapper;
import cn.zl.model.StreamProxy;
import cn.zl.model.StreamPush;
import cn.zl.model.VirtualVideoStream;
import com.sun.jna.CallbackThreadInitializer;
import com.sun.jna.Native;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 流上下线回调
 * - 流上下线回调，可以作为记录流行为、控制播放缓存等
 */
@Component
public class MKStreamChangeCallBack implements IMKStreamChangeCallBack {

    private static final Logger logger = LoggerFactory.getLogger(MKStreamChangeCallBack.class);

    @Resource
    private StreamManage streamManage;
    @Resource
    private StreamPushMapper streamPushMapper;
    @Resource
    private StreamProxyMapper streamProxyMapper;
    @Resource
    private VirtualVideoStreamMapper virtualVideoStreamMapper;

    public MKStreamChangeCallBack() {
        Native.setCallbackThreadInitializer(this, new CallbackThreadInitializer(true, false, "MediaStreamChangeThread"));
    }

    /**
     * 注册或反注册MediaSource事件广播
     *
     * @param regist 注册为1，注销为0
     * @param sender 该MediaSource对象
     */
    @Override
    public void invoke(int regist, MK_MEDIA_SOURCE sender) {
        ZLMApi ZLM_API = ZLMApiHelper.getInstance().getApi();
        //通过ZLM_API.mk_media_source_get_*可以查询流的各种信息
        String schema = ZLM_API.mk_media_source_get_schema(sender);
        String app = ZLM_API.mk_media_source_get_app(sender);
        String stream = ZLM_API.mk_media_source_get_stream(sender);
        if (regist == 1) {
            streamManage.add(app, stream, schema);
            logger.info("[流注册] schema:{} app:{} stream:{}", schema, app, stream);
        } else {
            streamManage.remove(app, stream, schema);
            logger.info("[流注销] schema:{} app:{} stream:{}", schema, app, stream);
        }
        int state = streamManage.getState(app, stream);
        Conditions where = Conditions.create("app", app).add("stream", stream);
        // 模拟视频流
        if (ZLConst.VIRTUAL_VIDEO.APP.equals(app)) {
            Optional<VirtualVideoStream> optional = virtualVideoStreamMapper.queryForObject(where);
            if (optional.isPresent()) {
                VirtualVideoStream virtualVideoStream = optional.get();
                if (virtualVideoStream.getState() != state) {
                    virtualVideoStream.setState(state);
                    virtualVideoStreamMapper.update(virtualVideoStream);
                }
            }
            return;
        }
        Optional<StreamProxy> streamProxyOptional = streamProxyMapper.queryForObject(where);
        if (streamProxyOptional.isPresent()) {
            StreamProxy streamProxy = streamProxyOptional.get();
            if (streamProxy.getState() != state) {
                streamProxy.setState(state);
                streamProxyMapper.update(streamProxy);
            }
            return;
        }
        Optional<StreamPush> streamPushOptional = streamPushMapper.queryForObject(where);
        if (streamPushOptional.isPresent()) {
            StreamPush streamPush = streamPushOptional.get();
            if (streamPush.getState() != state) {
                streamPush.setState(state);
                streamPushMapper.update(streamPush);
            }
        }
    }
}
